package blueBridgeCup;

import java.util.Scanner;

public class demo5 {
    static int ret=Integer.MAX_VALUE;
    static int ex,ey;
     static int m,n;
     static int[][]G;
     static int[]dx={0,0,1,-1};
     static int[]dy={1,-1,0,0};
     static boolean[][]memo;
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        m=in.nextInt();
        n=in.nextInt();
        G=new int[m][n];
        memo=new boolean[m][n];
        for (int i = 0; i <m ; i++) {
            for (int j = 0; j <n ; j++) {
                G[i][j]= in.nextInt();
            }
        }
        int sx= in.nextInt();
        int sy= in.nextInt();
         ex= in.nextInt();
         ey= in.nextInt();
         if(G[sx][sy]==0){
             System.out.println(-1);
             return;
         }
        dfs(sx,sy,0);
        System.out.println(ret==Integer.MAX_VALUE?-1:ret);

    }

    static void dfs(int i,int j,int count){
        if(memo[i][j])return;
        if(i==ex&&j==ey){
            ret=Math.min(ret,count+1);
            return;
        }
        memo[i][j]=true;
        for (int k = 0; k <4 ; k++) {
            int x=i+dx[k];
            int y=j+dy[k];
            if(x>=0&&x<m&&y>=0&&y<n&&G[x][y]!=0){
                dfs(x,y,count+1);
            }
        }
        memo[i][j]=false;
    }
}
